GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b2ff15...ebf9dd )
by Florian
01:14
created

Markers.newMarker   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
c 3
b 0
f 0
nc 9
nop 4
dl 0
loc 54
rs 7.8331

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 7 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $,
7
  Conversion, Cookies, Coordinates, Lines, Marker,
8
  id2alpha, mytrans, showAlert, trackMarker
9
*/
10
11
var Markers = {};
12
Markers.m_map = null;
13
Markers.m_markers = null;
14
15
16
Markers.init = function (themap) {
17
    'use strict';
18
19
    this.m_map = themap;
20
    this.m_markers = new Array(26 * 10);
21
22
    var id;
23
    for (id = 0; id !== this.m_markers.length; id = id + 1) {
24
        this.m_markers[id] = new Marker(this, id);
25
    }
26
};
27
28
29
Markers.getSize = function () {
30
    'use strict';
31
32
    return this.m_markers.length;
33
};
34
35
36
Markers.isValid = function (id) {
37
    'use strict';
38
39
    return 0 <= id && id < this.m_markers.length;
40
};
41
42
43
Markers.getById = function (id) {
44
    'use strict';
45
46
    if (id < 0 || id >= this.m_markers.length) {
47
        return null;
48
    }
49
50
    return this.m_markers[id];
51
};
52
53
54
Markers.getUsedMarkers = function () {
55
    'use strict';
56
57
    var count = 0;
58
    this.m_markers.map(function (m) {
59
        if (!m.isFree()) {
60
            count = count + 1;
61
        }
62
    });
63
    return count;
64
};
65
66
67
Markers.getFreeMarkers = function () {
68
    'use strict';
69
70
    return this.getSize() - this.getUsedMarkers();
71
};
72
73
74
Markers.getFreeId = function () {
75
    'use strict';
76
77
    var id;
78
    for (id = 0; id < this.m_markers.length; id = id + 1) {
79
        if (this.m_markers[id].isFree()) {
80
            return id;
81
        }
82
    }
83
    return -1;
84
};
85
86
87
Markers.getNextUsedId = function (id) {
88
    'use strict';
89
90
    var i;
91
    for (i = id + 1; i < this.m_markers.length; i = i + 1) {
92
        if (!this.m_markers[i].isFree()) {
93
            return i;
94
        }
95
    }
96
    return -1;
97
};
98
99
100
Markers.removeById = function (id) {
101
    'use strict';
102
103
    if (id >= 0 && id < this.m_markers.length) {
104
        this.m_markers[id].clear();
105
    }
106
};
107
108
109
Markers.deleteAll = function () {
110
    'use strict';
111
112
    this.m_markers.map(
113
        function (m) {
114
            m.clear();
115
        }
116
    );
117
};
118
119
120
Markers.saveMarkersList = function () {
121
    'use strict';
122
123
    var ids = [];
124
    this.m_markers.map(
125
        function (m) {
126
            if (!m.isFree()) {
127
                ids.push(m.getId());
128
            }
129
        }
130
    );
131
    Cookies.set('markers', ids.join(":"), {expires: 30});
132
};
133
134
135
Markers.toString = function () {
136
    'use strict';
137
138
    var parts = [];
139
    this.m_markers.map(
140
        function (m) {
141
            if (!m.isFree()) {
142
                parts.push(m.toString());
143
            }
144
        }
145
    );
146
    return parts.join("*");
147
};
148
149
150
Markers.update = function () {
151
    'use strict';
152
153
    this.m_markers.map(
154
        function (m) {
155
            m.update();
156
        }
157
    );
158
};
159
160
161
Markers.handleMarkerCleared = function () {
162
    'use strict';
163
164
    if (this.getUsedMarkers() === 0) {
165
        $('#btnmarkers2').hide();
166
    }
167
168
    this.saveMarkersList();
169
};
170
171
172
Markers.goto = function (id) {
173
    'use strict';
174
175
    trackMarker('goto');
176
177
    var m = this.getById(id);
178
    if (m) {
179
        this.m_map.setCenter(m.getPosition());
180
    }
181
};
182
183
184
Markers.center = function (id) {
185
    'use strict';
186
187
    trackMarker('center');
188
189
    var m = this.getById(id);
190
    if (m) {
191
        m.setPosition(this.m_map.getCenter());
192
    }
193
};
194
195
196
Markers.newMarker = function (coordinates, id, radius, name) {
197
    'use strict';
198
199
    radius = Math.max(radius, 0);
200
201
    if (id < 0 || id >= this.getSize() || !this.getById(id).isFree()) {
202
        id = this.getFreeId();
203
        if (id < 0) {
204
            showAlert(
205
                mytrans("dialog.error"),
206
                mytrans("dialog.toomanymarkers_error.content").replace(/%1/, Markers.getSize())
207
            );
208
            return null;
209
        }
210
    }
211
212
    var self = this,
213
        marker,
214
        div,
215
        nextid;
216
217
    if (!name) {
218
        name = "marker_" + id2alpha(id);
219
    }
220
221
    marker = this.getById(id);
222
    marker.initialize(this.m_map, name, coordinates, radius);
223
    div = this.createMarkerDiv(id);
224
225
    nextid = this.getNextUsedId(id);
226
    if (nextid < 0) {
227
        $('#dynMarkerDiv').append(div);
228
    } else {
229
        $(div).insertBefore('#dyn' + nextid);
230
    }
231
232
    $('#dyn' + id + ' > .edit').keydown(function (e) {
233
        if (e.which === 27) {
234
            self.leaveEditMode(id, false);
235
        } else if (e.which === 13) {
236
            self.leaveEditMode(id, true);
237
        }
238
    });
239
240
    $('#btnmarkers2').show();
241
    $('#btnmarkersdelete1').removeAttr('disabled');
242
    $('#btnmarkersdelete2').removeAttr('disabled');
243
244
    marker.update();
245
    this.saveMarkersList();
246
    Lines.updateLinesMarkerAdded();
247
248
    return marker;
249
};
250
251
252
253
254
Markers.createMarkerDiv = function (id) {
255
    'use strict';
256
257
    var iconw = 33,
258
        iconh = 37,
259
        offsetx = (id % 26) * iconw,
260
        offsety = Math.floor(id / 26) * iconh;
261
262
    return "<div id=\"dyn" + id + "\">" +
263
        "<table class=\"view\" style=\"width: 100%; vertical-align: middle;\">\n" +
264
        "    <tr>\n" +
265
        "        <td rowspan=\"3\" style=\"vertical-align: top\">\n" +
266
        "            <span style=\"width:" + iconw + "px; height:" + iconh + "px; float: left; display: block; background-image: url(img/markers.png); background-repeat: no-repeat; background-position: -" + offsetx + "px -" + offsety + "px;\">&nbsp;</span>\n" +
267
        "        </td>\n" +
268
        "        <td style=\"text-align: center\"><i class=\"fa fa-map-marker\"></i></td>\n" +
269
        "        <td class\"name\" colspan=\"2\">marker</td>\n" +
270
        "    </tr>\n" +
271
        "    <tr>\n" +
272
        "        <td style=\"text-align: center\"><i class=\"fa fa-globe\"></i></td>\n" +
273
        "        <td class=\"coords\" colspan=\"2\">N 48° 00.123 E 007° 51.456</td>\n" +
274
        "    </tr>\n" +
275
        "    <tr>\n" +
276
        "        <td style=\"text-align: center\"><i class=\"fa fa-circle-o\"></i></td>\n" +
277
        "        <td class=\"radius\">16100 m</td>\n" +
278
        "        <td>\n" +
279
        "            <div class=\"btn-group\" style=\"padding-bottom: 2px; padding-top: 2px; float: right\">\n" +
280
        "            <button class=\"my-button btn btn-mini btn-warning\" data-i18n=\"[title]sidebar.markers.edit_marker\" type=\"button\"  onclick=\"Markers.enterEditMode(" + id + ");\"><i class=\"fa fa-edit\"></i></button>\n" +
281
        "            <button class=\"my-button btn btn-mini btn-danger\" data-i18n=\"[title]sidebar.markers.delete_marker\" type=\"button\" onClick=\"Markers.removeById(" + id + ");\"><i class=\"fa fa-trash-o\"></i></button>\n" +
282
        "            <button class=\"my-button btn btn-mini btn-info\" data-i18n=\"[title]sidebar.markers.move_to\" type=\"button\" onClick=\"Markers.goto(" + id + ");\"><i class=\"fa fa-search\"></i></button>\n" +
283
        "            <button class=\"my-button btn btn-mini btn-warning\" data-i18n=\"[title]sidebar.markers.center\" type=\"button\" onClick=\"Markers.center(" + id + ");\"><i class=\"fa fa-crosshairs\"></i></button>\n" +
284
        "            <button class=\"my-button btn btn-mini btn-success\" data-i18n=\"[title]sidebar.markers.project\" type=\"button\" onClick=\"projectFromMarker(" + id + ");\"><i class=\"fa fa-location-arrow\"></i></button>\n" +
285
        "            </div>\n" +
286
        "        </td>\n" +
287
        "    </tr>\n" +
288
        "</table>\n" +
289
        "<table class=\"edit\" style=\"display: none; width: 100%; vertical-align: middle;\">\n" +
290
        "    <tr>\n" +
291
        "        <td rowspan=\"4\" style=\"vertical-align: top\"><span style=\"width:" + iconw + "px; height:" + iconh + "px; float: left; display: block; background-image: url(img/markers.png); background-repeat: no-repeat; background-position: -" + offsetx + "px -" + offsety + "px;\">&nbsp;</span>\n" +
292
        "        <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-map-marker\"></i></td>\n" +
293
        "        <td><input data-i18n=\"[title]sidebar.markers.name;[placeholder]sidebar.markers.name_placeholder\" class=\"name form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" +
294
        "    </tr>\n" +
295
        "    <tr>\n" +
296
        "        <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-globe\"></i></td>\n" +
297
        "        <td><input data-i18n=\"[title]sidebar.markers.coordinates;[placeholder]sidebar.markers.coordinates_placeholder\" class=\"coords form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" +
298
        "    </tr>\n" +
299
        "    <tr>\n" +
300
        "        <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-circle-blank\"></i></td>\n" +
301
        "        <td><input data-i18n=\"[title]sidebar.markers.radius;[placeholder]sidebar.markers.radius_placeholder\" class=\"radius form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" +
302
        "    </tr>\n" +
303
        "    <tr>\n" +
304
        "        <td colspan=\"2\" style=\"text-align: right\">\n" +
305
        "            <button class=\"btn btn-small btn-primary\" type=\"button\" onclick=\"Markers.leaveEditMode(" + id + ", true);\" data-i18n=\"dialog.ok\">OK</button>\n" +
306
        "            <button class=\"btn btn-small\" type=\"button\" onclick=\"Markers.leaveEditMode(" + id + ", false);\" data-i18n=\"dialog.cancel\">CANCEL</button>\n" +
307
        "        </td>\n" +
308
        "    </tr>\n" +
309
        "</table>" +
310
        "</div>";
311
};
312
313
314
Markers.enterEditMode = function (id) {
315
    'use strict';
316
317
    trackMarker('edit');
318
    var m = this.getById(id);
319
    if (!m) {
320
        return;
321
    }
322
323
    $('#dyn' + id + ' > .edit .name').val(m.getName());
324
    $('#dyn' + id + ' > .edit .coords').val(Coordinates.toString(m.getPosition()));
325
    $('#dyn' + id + ' > .edit .radius').val(m.getRadius());
326
327
    $('#dyn' + id + ' > .view').hide();
328
    $('#dyn' + id + ' > .edit').show();
329
};
330
331
332
Markers.leaveEditMode = function (id, takenew) {
333
    'use strict';
334
335
    if (!takenew) {
336
        $('#dyn' + id + ' > .view').show();
337
        $('#dyn' + id + ' > .edit').hide();
338
        return;
339
    }
340
341
    var m = this.getById(id),
342
        name = $('#dyn' + id + ' > .edit .name').val(),
343
        s_coords = $('#dyn' + id + ' > .edit .coords').val(),
344
        s_radius = $('#dyn' + id + ' > .edit .radius').val(),
345
        name_ok = /^([a-zA-Z0-9-_]*)$/.test(name),
346
        coords = Coordinates.fromString(s_coords),
347
        radius = Conversion.getInteger(s_radius, 0, 100000000000),
348
        errors = [];
349
350
    if (!name_ok) {
351
        errors.push(mytrans("sidebar.markers.error_badname").replace(/%1/, name));
352
    }
353
    if (!coords) {
354
        errors.push(mytrans("sidebar.markers.error_badcoordinates").replace(/%1/, s_coords));
355
    }
356
    if (radius === null) {
357
        errors.push(mytrans("sidebar.markers.error_badradius").replace(/%1/, s_radius));
358
    }
359
360
    if (errors.length > 0) {
361
        showAlert(mytrans("dialog.error"), errors.join("<br /><br />"));
362
        return;
363
    }
364
365
    m.setNamePositionRadius(name, coords, radius);
366
    $('#dyn' + id + ' > .view').show();
367
    $('#dyn' + id + ' > .edit').hide();
368
};
369